package 课设;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class userAdd extends JFrame {
    private JTextField username,password,email;
    private JRadioButton sexfemale,sexmale;
    private JPanel sex,birth;
    private JTextField year;
    private JComboBox<Integer> month,day;
    private JButton ok,cancel;

    public userAdd(String name) {
        super();
        this.setSize(400,330);
        this.setTitle("新用户");
        this.setLocationRelativeTo(getOwner());//居中
        //设置组件布局
        Container cont = getContentPane();
        cont.setLayout(new GridLayout(7,2));
        //添加组件
        //------begin------
        //用户名
        cont.add(new JLabel("账户"));
        username = new JTextField(10);
        cont.add(username);
        //用户名
        cont.add(new JLabel("密码"));
        password = new JTextField(10);
        cont.add(password);
        //性别
        cont.add(new JLabel("性别"));
        sexmale = new JRadioButton("男",true);
        sexfemale = new JRadioButton("女");
        ButtonGroup bg = new ButtonGroup();
        bg.add(sexmale);
        bg.add(sexfemale);
        sex = new JPanel(new GridLayout(1,2));
        sex.add(sexmale);
        sex.add(sexfemale);
        cont.add(sex);
        //出生日期
        cont.add(new JLabel("出生日期"));
        year = new JTextField(4);
        month = new JComboBox<Integer>();
        int i;
        for(i=1;i<=12;i++)
            month.addItem(i);
        day = new JComboBox<Integer>();
        for(i=1;i<=31;i++)
            day.addItem(i);
        birth = new JPanel();
        birth.add(year);
        birth.add(new JLabel("-"));
        birth.add(month);
        birth.add(new JLabel("-"));
        birth.add(day);
        cont.add(birth);
        //邮箱
        cont.add(new JLabel("邮箱"));
        email = new JTextField(10);
        cont.add(email);

        //按钮
        ok = new JButton("添加");
        cancel = new JButton("取消");
        cont.add(ok);
        cont.add(cancel);

        //注册监听器
        ok.addActionListener(new ActionListener() { //___________.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                db dbcon = new db();
                try {
                    String sql = "insert into users values(?,?)";
                    PreparedStatement prestate = dbcon.PreparedStatement(sql);  //PreparedStatement __________ = dbcon.PreparedStatement(sql);
                    String s = username.getText();
                    prestate.setString(1, s);
                    prestate.setString(2, password.getText());
                    prestate.executeUpdate();

                    String sq = "insert into detailed_user values(?,?,?)";
                    //ResultSet rs = dbcon.executeQuery("select top 1 * from users order by userId desc");
                    PreparedStatement p = dbcon.PreparedStatement(sq);  //PreparedStatement __________ = dbcon.PreparedStatement(sql);
                    //p.setInt(1, rs.getInt("userId"));
                    p.setString(1, sexmale.isSelected()?sexmale.getText():sexfemale.getText());
                    p.setInt(2, 2023 - Integer.parseInt(year.getText()));
                    p.setString(3, email.getText());
                    p.executeUpdate();
                    JOptionPane.showMessageDialog(null,"插入成功!");
                    blog_show psel = new blog_show(s);
                    dispose();
                    psel.setVisible(true);
                } catch(SQLException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public userAdd(String name, int f) {
        super();
        this.setSize(400,330);
        this.setTitle("新用户");
        this.setLocationRelativeTo(getOwner());//居中
        //设置组件布局
        Container cont = getContentPane();
        cont.setLayout(new GridLayout(7,2));
        //添加组件
        //------begin------
        //用户名
        cont.add(new JLabel("账户"));
        username = new JTextField(10);
        cont.add(username);
        //用户名
        cont.add(new JLabel("密码"));
        password = new JTextField(10);
        cont.add(password);
        //性别
        cont.add(new JLabel("性别"));
        sexmale = new JRadioButton("男",true);
        sexfemale = new JRadioButton("女");
        ButtonGroup bg = new ButtonGroup();
        bg.add(sexmale);
        bg.add(sexfemale);
        sex = new JPanel(new GridLayout(1,2));
        sex.add(sexmale);
        sex.add(sexfemale);
        cont.add(sex);
        //出生日期
        cont.add(new JLabel("出生日期"));
        year = new JTextField(4);
        month = new JComboBox<Integer>();
        int i;
        for(i=1;i<=12;i++)
            month.addItem(i);
        day = new JComboBox<Integer>();
        for(i=1;i<=31;i++)
            day.addItem(i);
        birth = new JPanel();
        birth.add(year);
        birth.add(new JLabel("-"));
        birth.add(month);
        birth.add(new JLabel("-"));
        birth.add(day);
        cont.add(birth);
        //邮箱
        cont.add(new JLabel("邮箱"));
        email = new JTextField(10);
        cont.add(email);

        //按钮
        ok = new JButton("添加");
        cancel = new JButton("取消");
        cont.add(ok);
        cont.add(cancel);

        //注册监听器
        ok.addActionListener(new ActionListener() { //___________.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                db dbcon = new db();
                try {
                    String sql = "insert into users values(?,?)";
                    PreparedStatement prestate = dbcon.PreparedStatement(sql);  //PreparedStatement __________ = dbcon.PreparedStatement(sql);
                    String s = username.getText();
                    prestate.setString(1, s);
                    prestate.setString(2, password.getText());
                    prestate.executeUpdate();

                    String sq = "insert into detailed_user values(?,?,?)";
                    //ResultSet rs = dbcon.executeQuery("select top 1 * from users order by userId desc");
                    PreparedStatement p = dbcon.PreparedStatement(sq);  //PreparedStatement __________ = dbcon.PreparedStatement(sql);
                    //p.setInt(1, rs.getInt("userId"));
                    p.setString(1, sexmale.isSelected()?sexmale.getText():sexfemale.getText());
                    p.setInt(2, 2023 - Integer.parseInt(year.getText()));
                    p.setString(3, email.getText());
                    p.executeUpdate();
                    JOptionPane.showMessageDialog(null,"插入成功!");
                    usertable psel = new usertable();
                    dispose();
                    psel.setVisible(true);
                } catch(SQLException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public static void main(String[] agrs) {
        userAdd w = new userAdd("张三");
        w.setVisible(true);
    }
}
